package com.widget;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.AppCompatSpinner;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TimePicker;

import com.landmesure.bluetoothchat.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

public class CustomWidget {
    private Context mContext;
    private SpinnerAdapter mAdapter;
    public CustomWidget(Context context) {
        mContext = context;
    }

    /**
     * 时间控件
     */
    public void timeWidget(final EditText et) {
        Calendar calendar = Calendar.getInstance();
        new TimePickerDialog(mContext,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        String hour = hourOfDay < 10 ? "0" + hourOfDay : String.valueOf(hourOfDay);
                        String min = minute < 10 ? "0" + minute : String.valueOf(minute);
                        et.setText(String.format(mContext.getResources().getString(R.string.day_time),
                                hour, min));
                    }
                },
                calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE),
                true
        ).show();
    }

    /**
     * 日期控件
     */
    public void dateWidget(final EditText et) {
        final Calendar calendar = Calendar.getInstance();
        final DatePickerDialog datePickerDialog = new DatePickerDialog(mContext,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        /*calendar.set(Calendar.YEAR, year);
                        calendar.set(Calendar.MONTH, month);
                        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        new TimePickerDialog(mContext,
                                new TimePickerDialog.OnTimeSetListener() {
                                    @Override
                                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                                        calendar.set(Calendar.MINUTE, minute);
                                        et.setText(
                                                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                                                        Locale.getDefault()).format(calendar.getTimeInMillis())
                                        );
                                    }
                                },
                                calendar.get(Calendar.HOUR_OF_DAY),
                                calendar.get(Calendar.MINUTE),
                                true
                        ).show();*/
                    }
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
        );
        datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                calendar.set(Calendar.YEAR, datePickerDialog.getDatePicker().getYear());
                calendar.set(Calendar.MONTH, datePickerDialog.getDatePicker().getMonth());
                calendar.set(Calendar.DAY_OF_MONTH, datePickerDialog.getDatePicker().getDayOfMonth());
                new TimePickerDialog(mContext,
                        new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                                calendar.set(Calendar.MINUTE, minute);
                                et.setText(
                                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                                                Locale.getDefault()).format(calendar.getTimeInMillis())
                                );
                            }
                        },
                        calendar.get(Calendar.HOUR_OF_DAY),
                        calendar.get(Calendar.MINUTE),
                        true
                ).show();
            }
        });
        datePickerDialog.show();
    }

    /**
     * 下拉控件
     */
    public AppCompatSpinner spinnerWidget(LayoutInflater inflater, ArrayList<String> list, int selection) {
        SpinnerAdapter adapter = new SpinnerAdapter(inflater, list, selection);
        mAdapter = adapter;
        AppCompatSpinner spinner = new AppCompatSpinner(mContext);
        LinearLayout.LayoutParams params = new LinearLayout
                .LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        params.setMargins(5, 5, -5, 5);
        spinner.setLayoutParams(params);
        spinner.setAdapter(adapter);
        return spinner;
    }

    /**
     * 返回下拉控件回调接口实例
     */
    public SpinnerAdapter getAdapter() {
        return mAdapter;
    }
}
